package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.entity.UserFavoritesInfo;
import com.example.computer_exam_training_sys.service.UserFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/favorites")
@CrossOrigin
public class UserFavoritesController {

    @Autowired
    private UserFavoritesService service;

    //查询所有收藏记录
    //http://localhost:8080/favorites/all
    @GetMapping("/all")
    public List<UserFavoritesInfo> findAll() {
        return service.findAll();
    }

    //根据用户ID查询其所有收藏题目
    //http://localhost:8080/favorites/byuser?userId=1
    @GetMapping("/byuser")
    public List<UserFavoritesInfo> findByUserId(@RequestParam Integer userId) {
        return service.findByUserId(userId);
    }

    //根据收藏记录ID查询详情（一般用于调试）
    //http://localhost:8080/favorites/byid?id=3
    @GetMapping("/byid")
    public UserFavoritesInfo findById(@RequestParam Integer id) {
        return service.findById(id);
    }


    //添加收藏
    //http://localhost:8080/favorites/add
    //测试json语句{"userId": 1,"questionId": 2}
    @PostMapping("/add")
    public int add(@RequestBody UserFavoritesInfo favorite) {
        return service.insert(favorite);
    }

    //根据收藏记录ID删除
    //http://localhost:8080/favorites/delete?id=5
    @GetMapping("/delete")
    public int deleteById(@RequestParam Integer id) {
        return service.deleteById(id);
    }

    //根据用户ID和题目ID联合删除（用于用户取消收藏）
    //http://localhost:8080/favorites/deletebyuq?userId=1&questionId=2
    @GetMapping("/deletebyuq")
    public int deleteByUserAndQuestion(@RequestParam Integer userId, @RequestParam Integer questionId) {
        return service.deleteByUserAndQuestion(userId, questionId);
    }

    // 新增接口：判断用户是否已收藏某题
    // http://localhost:8080/favorites/exists?userId=1&questionId=1
    @GetMapping("/exists")
    public boolean existsByUserAndQuestion(@RequestParam Integer userId, @RequestParam Integer questionId) {
        return service.existsByUserAndQuestion(userId, questionId);
    }

    @GetMapping("/query")
    public List<Map<String, Object>> queryFavorites(
            @RequestParam Integer userId,
            @RequestParam(required = false) Integer courseId,
            @RequestParam(required = false) Integer pointId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime
    ) {
        return service.findFavoritesWithDetail(userId, courseId, pointId, startTime, endTime);
    }

    // 批量根据收藏id查询收藏题目详情
// http://localhost:8080/favorites/byids?ids=1,2,3
    @GetMapping("/byids")
    public List<Map<String, Object>> findFavoritesByIds(@RequestParam String ids) {
        // 解析ids字符串为List<Integer>
        List<Integer> idList =Arrays.stream(ids.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        // 你需要在service/mapper层实现findFavoritesByIds方法，返回题目详情（可参考findFavoritesWithDetail的多表查询写法）
        return service.findFavoritesByIds(idList);
    }
}
